﻿Imports System.Data.OleDb
Public Class MyProfile
    Inherits System.Web.UI.Page

    Public Shared cakepointsok As Boolean = False
    Public Shared totalcakesok As Boolean = False
    Public Shared averagecakeok As Boolean = False
    Public Shared cakecomments As Boolean = False
    Public Shared tipcountok As Boolean = False
    Public Shared tiplikeok As Boolean = False

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Check if this is the first time they are on this page by checking the list of users if first time is checked
        If User.Identity.IsAuthenticated Then
            'Do Nothing
        Else
            Response.Redirect("opps.aspx?id=12")
        End If

        lbl_warning.Visible = False

        lbl_name.Text = User.Identity.Name
        If Request.Params("ft") = 1 Then
            Dim SQL As String = "SELECT * FROM UserPictures WHERE UserName = @f1"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
            cmd.Parameters.AddWithValue("@f1", Class1.getUserNumber(User.Identity.Name))

            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader
            dr.Read()
            If dr.HasRows Then
                'User already exists
            Else
                'Add User
                Dim SQL2 As String = "INSERT INTO UserPictures (UserName,ProfilePic,RoleOfUser) VALUES (@f1,@f2,@F3)"
                Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
                Dim cmd2 As OleDbCommand = New OleDbCommand(SQL2, conn2)
                cmd2.Parameters.AddWithValue("@f1", Class1.getUserNumber(User.Identity.Name))
                cmd2.Parameters.AddWithValue("@f2", "Pictures/Profile/Unknown.png")
                cmd2.Parameters.AddWithValue("@f3", "Bronze")
                conn2.Open()
                cmd2.ExecuteNonQuery()
                conn2.Close()
                cmd2.Dispose()
                conn2.Dispose()
                Dim un As String
                un = User.Identity.Name
                Roles.AddUserToRole(un, "Bronze")

            End If
            dr.Close()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
        End If

        Dim SQL3 As String = "SELECT * FROM ProfilePage WHERE UserName = @F1"
        Dim conn3 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd3 As OleDbCommand = New OleDbCommand(SQL3, conn3)
        cmd3.Parameters.AddWithValue("@f1", User.Identity.Name)
        conn3.Open()
        Dim dr2 As OleDbDataReader = cmd3.ExecuteReader
        dr2.Read()
        If dr2.HasRows Then
            play.Text = ""
        Else
            play.Text = "You are still in trail mode. To get your Stats and be able to change your profile picture you must add a cake and a comment"
        End If
    End Sub

    Function getcpprogress(ByVal cakepoints As Integer, ByVal role As String)
        Dim myoutput As String = ""
        If role = "Gold" Then
            myoutput = "Maxed Out"
        End If

        If role = "Bronze" Then
            Dim i As Integer
            i = (cakepoints / 50) * 100
            myoutput = "You are " & i & "% towards the needed 50 points"
            If i >= 100 Then cakepointsok = True
        End If

        If role = "Silver" Then
            Dim i As Integer
            i = (cakepoints / 150) * 100
            myoutput = "You are " & i & "% towards the needed 150 points"
            If i >= 100 Then cakepointsok = True
        End If

        Return myoutput

    End Function

    Function getcommentp(ByVal commentcount As Integer, ByVal role As String)
        Dim myoutput As String = ""
        If role = "Gold" Then
            myoutput = "Maxed Out"
        End If

        If role = "Bronze" Then
            Dim i As Integer
            i = (commentcount / 5) * 100
            myoutput = "You are " & i & "% towards the required 5 Comments"
            If i >= 100 Then cakecomments = True
        End If

        If role = "Silver" Then
            Dim i As Integer
            i = (commentcount / 20) * 100
            myoutput = "You are " & i & "% towards the required 20 Comments"
            If i >= 100 Then cakecomments = True
        End If

        Return myoutput
    End Function

    Function getcakep(ByVal cakecount As Integer, ByVal role As String)
        Dim myoutput As String = ""
        If role = "Gold" Then
            myoutput = "Maxed Out"
        End If

        If role = "Bronze" Then
            Dim i As Integer
            i = (cakecount / 3) * 100
            myoutput = "You are " & i & "% towards the required 3 Cakes"
            If i >= 100 Then totalcakesok = True
        End If

        If role = "Silver" Then
            Dim i As Integer
            i = (cakecount / 10) * 100
            myoutput = "You are " & i & "% towards the required 10 Cakes"
            If i >= 100 Then totalcakesok = True
        End If

        Return myoutput
    End Function

    Function getavg(ByVal avg As Integer, ByVal role As String)
        Dim myoutput As String = ""
        If role = "Gold" Then
            myoutput = "Maxed Out"
        End If

        If role = "Bronze" Then
            Dim i As Integer
            i = (avg / 3) * 100
            myoutput = "You are " & i & "% towards having an average cake rating of 3"
            If i >= 100 Then averagecakeok = True
        End If

        If role = "Silver" Then
            Dim i As Integer
            i = (avg / 4) * 100
            myoutput = "You are " & i & "% towards having an average cake rating of 4"
            If i >= 100 Then averagecakeok = True
        End If

        Return myoutput
    End Function

    Function showavergae(ByVal a As Integer)
        Return a
    End Function


    Function tipsection(ByVal userid As Integer, ByVal role As String)
        If role = "Silver" Then
            Dim tipcount As Integer = getTipCount()
            Dim tipCcount As Integer = getTipCCount()
            Dim i As Integer
            Dim x As Integer
            i = (tipcount / 10) * 100
            x = (tipCcount / 25) * 100
            Dim myoutput As String
            myoutput = "You are " & i & " % towards having the required 10 tips <br/>You are " & x & "% towards having 25 people like your tips"
            If i >= 100 Then tipcountok = True
            If x >= 100 Then tiplikeok = True
            Return myoutput
        ElseIf role = "Gold" Then
            Return "Tips maxed out"
        Else
            Return "At Silver and above your Tip Progress will be displayed Here"

        End If
        Return ""
    End Function

    Function getTipCount()
        Dim SQL As String = "SELECT * FROM TipCount WHERE Author = @f1"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        cmd.Parameters.AddWithValue("@f1", Class1.getUserNumber(User.Identity.Name))
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        dr.Read()
        If dr.HasRows() Then
            Return dr.GetValue(1)
        Else
            Return 0
        End If
    End Function

    Function getTipCCount()
        Dim SQL As String = "SELECT * FROM TipLikeCount WHERE Author = @f1"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        cmd.Parameters.AddWithValue("@f1", Class1.getUserNumber(User.Identity.Name))
        conn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader
        dr.Read()
        If dr.HasRows() Then
            Return dr.GetValue(1)
        Else
            Return 0
        End If
    End Function

    Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand

        Dim rep As RepeaterItem = e.Item

        Dim myfileupload As FileUpload = TryCast(rep.FindControl("FileUpload1"), FileUpload)

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(myfileupload.FileName)
        myfileupload.SaveAs(Server.MapPath("Pictures/Profile") & "/" & newFileName)
        
        Dim SQL As String = "UPDATE UserPictures SET ProfilePic = @f1 WHERE UserName = @f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        cmd.Parameters.AddWithValue("@f1", "Pictures/Profile/" & newFileName)
        cmd.Parameters.AddWithValue("@f2", Class1.getUserNumber(User.Identity.Name))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("MyProfile.aspx")

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub btn_cashIn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_cashIn.Click
        If User.IsInRole("Bronze") Then
            'Check if user can go to Silver
            If cakepointsok And totalcakesok And averagecakeok And cakecomments Then
                'User can Cash in to Silver
                Roles.RemoveUserFromRole(User.Identity.Name, "Bronze")
                Roles.AddUserToRole(User.Identity.Name, "Silver")
                updatetable("Silver")
                Response.Redirect("MyProfile.aspx")

            Else
                play.Text = "Check you score you can not cash in yet"
            End If
        End If

        If User.IsInRole("Silver") Then
            'Check if user can go to Gold
            If cakepointsok And totalcakesok And averagecakeok And cakecomments And tiplikeok And tipcountok Then
                Roles.RemoveUserFromRole(User.Identity.Name, "Silver")
                Roles.AddUserToRole(User.Identity.Name, "Gold")
                updatetable("Gold")
                Response.Redirect("MyProfile.aspx")
            Else
                play.Text = "Check you score you can not cash in yet"
            End If
        End If

        If User.IsInRole("Gold") Then
            play.Text = "You are already Gold. Well done!"
        End If

    End Sub

    Function updatetable(ByVal role As String)
        Dim SQL = "UPDATE UserPictures SET RoleOfUser=@f1 WHERE UserName=@f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CakeConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(SQL, conn)
        cmd.Parameters.AddWithValue("@f1", role)
        cmd.Parameters.AddWithValue("@f2", Class1.getUserNumber(User.Identity.Name))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Return True
    End Function
End Class